$(function() {
	getRoleList();
	$('#addRole').on('hide.bs.modal', function () {
		$('#addRoleForm')[0].reset()
	});
	$('#editRole').on('hide.bs.modal', function () {
		$('#editRoleForm')[0].reset()
	});
	$('#assignRoleMenu').on('hide.bs.modal', function () {
		$('#assignRoleMenuForm')[0].reset()
	});
 });

function cellStyle(value, row, index) {
	  var classes = ['active', 'success', 'info', 'warning', 'danger'];

	  if (index % 2 === 0 && index / 2 < classes.length) {
	    return {
	      classes: classes[index / 2]
	    };
	  }
	  return {};
	}

	function rowStyle(row, index) {
	  var classes = [ 'success', 'info', 'warning', 'danger'];

	  if (index % 2 === 0 && index / 2 < classes.length) {
	    return {
	      classes: classes[index / 2]
	    };
	  }
	  return {};
	}

//获取用户列表    
function getRoleList(){
	$('#roleList').bootstrapTable({
		method: 'post',      //请求方式（*）
		contentType:"application/x-www-form-urlencoded; charset=UTF-8",
		url:"/sysRole/getRoleList",
	    striped: true,      //是否显示行间隔色
	    cache: false,      //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
	    sortable: true,      //是否启用排序
	    showRefresh: false,  //显示刷新按钮  
	    striped: true,        //是否显示行间隔色
	    sortOrder: "asc",          //排序方式 
	    pagination: true,          //是否显示分页（*）
	    sidePagination: "server",      //分页方式：client客户端分页，server服务端分页（*）
	    minimumCountColumns: 2,    //最少允许的列数
	    clickToSelect: true,    //是否启用点击选中行
	    uniqueId: "id",      //每一行的唯一标识，一般为主键列
	    showToggle:false,     //是否显示详细视图和列表视图的切换按钮
	    queryParams:queryParams,
	    columns: [{
	    	title:'checked',
	    	checkbox:true
	    },{  
    	    field: 'Number',  
    	    title: '序号',  
    	    align: 'center',
    	    formatter: function (value, row, index) {  
    	    	var pageSize = $('#roleList').bootstrapTable('getOptions').pageSize;     //通过table的#id 得到每页多少条
			    var pageNumber = $('#roleList').bootstrapTable('getOptions').pageNumber; //通过table的#id 得到当前第几页
			    return pageSize * (pageNumber - 1) + index + 1;    // 返回每条的序号： 每页条数 *（当前页 - 1 ）+ 序号
    	    }  
    	},{
	        field: 'roleName',
	        align: 'center',
	        title: '角色名称'
	    },{
	        field: 'roleCode',
	        align: 'center',
	        title: '角色编码'
	    },{
	        field: 'status',
	        align: 'center',
	        title: '状态',
        	formatter: function (value, row, index) {  
        		if(value==1){
        			return "正常";  
        		}else if(value==2){
        			return "暂存";
        		}
     	    }  	
	    },{
	        field: 'createDate',
	        align: 'center',
	        title: '创建时间',
	        formatter: function (value, row, index) {  
	        	return getFormatDateByLong(value,'yyyy-MM-dd hh:mm:ss');  
     	    }  
	    },{
	        field: 'id',
	        align: 'center',
	        title: '操作',
	        formatter : function(value, row, index) {
				var e = '<a class="btn btn-info btn-xs" href="#" mce_href="#" title="详情" onclick="detailRole(\''+ value+ '\')"><i class="fa fa-paste"></i> 详情</a>  ';
				var m = '<a class="btn btn-warning btn-xs" href="#" mce_href="#" title="编辑" onclick="editRole(\''+ value+ '\')"><i class="fa fa-paste"></i> 编辑</a>  ';
				var h = '<a class="btn btn-danger btn-xs" href="#" mce_href="#" title="删除" onclick="deleteRole(\''+ value+ '\')"><i class="fa fa-trash-o"></i> 删除</a>  ';
				return m+e+h;
			}
	    }]
	});
	function queryParams(params) {   //设置查询参数
		return{
			pageNum:(params.offset/params.limit)+1,   // 页码
			pageSize: params.limit, //每页多少条数据
        }
    }
}

function addRole(){
   	 $('#addRole').modal({
            keyboard: true
     });
}

function detailRole(roleId){
	 $('#detailRole').modal({
         keyboard: true
     });
	$.ajax({
		url:"/sysRole/getSysRoleById",
		type:"POST",
		data:{
			"id":roleId
		},
		dataType:"json",
		success:function(data){
			if(data!=null){
				$("#detailId").val(data.id);
				$("#detailRoleName").val(data.roleName);
				$("#detailRoleCode").val(data.roleCode);
				$("#detailStatus option[value='"+data.status+"']").attr("selected","selected");  
			}
		}
	});
}

function editRole(id){
/*	var getSelectRows = $("#roleList").bootstrapTable('getSelections', function (row) {
		return row;
	});
	if(getSelectRows.length==0){
		layer.msg("请选择一条数据");
		return false;
	}
	if(getSelectRows.length>1){
		layer.msg("请选择一条数据");
		return false;
	}*/
	 $('#editRole').modal({
         keyboard: true
     });
	$.ajax({
		url:"/sysRole/getSysRoleById",
		type:"POST",
		data:{
			"id":id
		},
		dataType:"json",
		success:function(data){
			if(data!=null){
				$("#updateId").val(data.id);
				$("#updateRoleName").val(data.roleName);
				$("#updateRoleCode").val(data.roleCode);
				$("#updateStatus option[value='"+data.status+"']").attr("selected","selected");  
			}
		}
	});
}


function deleteRole(id){
	//删除询问
	layer.confirm('确定删除该角色吗？', {
		  btn: ['确定','取消'] //按钮
		}, function(){
			$.ajax({
				url:"/sysRole/removeById",
				type:"POST",
				data:{
					"id":id
				},
				dataType:"json",
				success:function(data){
					if(data!=null){
						layer.msg(data.msg);
						$("#roleList").bootstrapTable('refresh', {url: '/sysRole/getRoleList'});  
					}
				}
			});
		});
}

function search(){
	var roleName=$("#selectRoleName").val();
	var status=$("#selectStatus").val();
	$("#roleList").bootstrapTable('refresh', {url: '/sysRole/getRoleList',query:{roleName:roleName,status:status}}); 
}

function resets(){
	$("#roleList").bootstrapTable('refresh', {url: '/sysRole/getRoleList'}); 
	$("#selectRoleName").val("");
	$("#selectStatus").val("");
}

function menuToRole(id,roleName){
	var getSelectRows = $("#roleList").bootstrapTable('getSelections', function (row) {
		return row;
	});
	if(getSelectRows.length==0){
		layer.msg("请选择一条数据");
		return false;
	}
	if(getSelectRows.length>1){
		layer.msg("只能选择一条数据");
		return false;
	}
	$('#assignRoleMenu').modal({
        keyboard: true
    });
	$("#roleId").val(getSelectRows[0].id);
	$("#assignRoleName").val(getSelectRows[0].roleName);
	getMenuTreeData();
}

function setMenu(){
	getAllSelectNodes();
}
